package com.abbas.smartCity.ui.parkingWhere;

import android.app.DatePickerDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;

import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.parkingWhereBean.ParkingHistoryList;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MListView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import retrofit2.Call;

public class ParkingHistoryActivity extends BaseActivity {

    private Button in;
    private Button out;
    private ImageButton search;
    private MListView lv;
    private Button loadmore;

    private String inTime = "";
    private String outTime = "";
    private int pageNum = 1;
    private int pageSize = 5;
    private ListCommAdapter<ParkingHistoryList.RowsBean, ListCommAdapter.ParkingHistoryHolder> listAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_parking_history;
    }

    @Override
    protected String setCustomTitle() {
        return "停车记录";

    }

    @Override
    protected void initView() {
        in = (Button) findViewById(R.id.in);
        out = (Button) findViewById(R.id.out);
        search = (ImageButton) findViewById(R.id.search);
        lv = (MListView) findViewById(R.id.lv);
        loadmore = (Button) findViewById(R.id.loadmore);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final DatePickerDialog datePickerDialog = new DatePickerDialog(ParkingHistoryActivity.this);
                datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        final Calendar instance = Calendar.getInstance();
                        instance.set(year, month, dayOfMonth);
                        in.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(instance.getTime()));
                    }
                });
                datePickerDialog.show();
            }
        });

        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final DatePickerDialog datePickerDialog = new DatePickerDialog(ParkingHistoryActivity.this);
                datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        final Calendar instance = Calendar.getInstance();
                        instance.set(year, month, dayOfMonth);
                        out.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(instance.getTime()));
                    }
                });
                datePickerDialog.show();
            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum = 1;
                getParkingList();
            }
        });

        loadmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum += 1;
                getParkingList();
            }
        });

        getParkingList();
    }

    private void getParkingList() {
        api.getParkingHistoryList(inTime, outTime, pageNum, pageSize).enqueue(new ICallback<ParkingHistoryList>() {
            @Override
            public void onSuccess(Call<ParkingHistoryList> call, ParkingHistoryList data) {
                if (listAdapter == null){
                    listAdapter = new ListCommAdapter<ParkingHistoryList.RowsBean, ListCommAdapter.ParkingHistoryHolder>(
                            data.getRows(),
                            ListCommAdapter.ParkingHistoryHolder.class,
                            R.layout.parking_history_item
                    ) {
                        @Override
                        protected void show(ParkingHistoryList.RowsBean item, View convertView, ParkingHistoryHolder viewHolder, int position, Context context) {
                            viewHolder.name.setText(item.getParkName());
                            viewHolder.tag1.setText("车牌号："+item.getParkNo());
                            viewHolder.tag2.setText("入场时间："+item.getEntryTime());
                            viewHolder.tag3.setText("出场时间："+item.getOutTime());
                            viewHolder.money.setText("￥"+item.getMonetary());
                        }
                    };
                    lv.setAdapter(listAdapter);
                }else {
                    if (pageNum == 1){
                        listAdapter.resetData(data.getRows());
                    }else {
                        listAdapter.append(data.getRows());
                    }
                }
            }
        });
    }
}